/*
 * DeleteMember.java
 *
 * Created on November 30, 2005, 10:54 AM
 */

package org.volume4.alliedbridge.model.person;

import java.io.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class DeleteMember extends HttpServlet {

    public String DRIVER, URL, USER, PASS;
    public String app_root,messageSuccess,messageFailed,mem_number,mem_status,_table,_lbtable,message,file_prefix;

    public void init() throws ServletException {
        ServletContext context = getServletContext();
        DRIVER = context.getInitParameter("driver");
        URL = context.getInitParameter("mc_dburl");
        USER = context.getInitParameter("mc_dbuser");
        PASS = context.getInitParameter("mc_dbpass");
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        Connection con = null;
        messageFailed = "Process to delete member failed.";

        try {
            Class.forName(DRIVER);
            con = DriverManager.getConnection(URL, USER, PASS);

            //Get all parameters from request.
            mem_number = request.getParameter("mem_number");
            mem_status = request.getParameter("mem_status");

            if (mem_status.equalsIgnoreCase("Active")) {
                _table = "ab_members";
                _lbtable = "ab_leader_board";
            } else {
                _table = "ab_members_pending";
                _lbtable = "ab_leader_board_pending";
            }

            String getMemberData = "SELECT mem_number,sex,banner1,banner2,banner3,img FROM " + _table +
            " WHERE mem_number= '" + mem_number + "'";

            PreparedStatement getDataStmt = con.prepareStatement(getMemberData);
            ResultSet rs = getDataStmt.executeQuery();
            boolean hasData = false;

            while (rs.next()) {
                hasData = true;

                String mem_number = rs.getString(1);
                String sex = rs.getString(2);
                String banner1 = rs.getString(3);
                String banner2 = rs.getString(4);
                String banner3 = rs.getString(5);
                String img = rs.getString(6);

                String removeMemberData = "DELETE FROM " + _table +
                        " WHERE mem_number= '" + mem_number + "'";

                String removeLBData = "DELETE FROM " + _lbtable +
                        " WHERE mem_number= '" + mem_number + "'";

                PreparedStatement removeDataStmt = con.prepareStatement(removeMemberData);
                int removeDataResult = removeDataStmt.executeUpdate(removeMemberData);

                PreparedStatement removeLBDataStmt = con.prepareStatement(removeLBData);
                int removeLBDataResult = removeLBDataStmt.executeUpdate(removeLBData);

                file_prefix = "/opt/tomcat5/webapps/mc/";

                if (!banner1.equalsIgnoreCase("")) {
                    System.out.println(banner1);
                    System.out.println("Deleting banner1");
                    File selectedFile = new File(file_prefix + banner1.substring(3));
                    System.out.println(selectedFile);
                    selectedFile.delete();
                } else if (!banner2.equalsIgnoreCase("")) {
                    File selectedFile = new File(file_prefix + banner2.substring(3));
                    System.out.println(selectedFile);
                    selectedFile.delete();
                } else if (!banner3.equalsIgnoreCase("")) {
                    File selectedFile = new File(file_prefix + banner3.substring(3));
                    System.out.println(selectedFile);
                    selectedFile.delete();
                } else if ((!img.equalsIgnoreCase("")) || (!img.contains("no_image.gif"))) {
                    File selectedFile = new File(file_prefix + img.substring(3));
                    System.out.println(selectedFile);
                    selectedFile.delete();
                }

                app_root = request.getContextPath();
                messageSuccess = "Member " + mem_number + ", was successfuly deleted from the website.";
                response.sendRedirect(app_root + "/application/application_messages/process_result.jsp?message=" + messageSuccess);
            }
            if(!hasData) {
                app_root = request.getContextPath();
                response.sendRedirect(app_root + "/application/application_messages/process_result.jsp?message=" + messageFailed);
            }

        } catch (SQLException ex) {
            System.out.println("\nERROR:------ SQLException -----\n");
            while (ex != null) {
                System.out.println("Message: " + ex.getMessage());
                System.out.println("SQLState: " + ex.getSQLState());
                System.out.println("ErrorCode :" + ex.getErrorCode());
                ex = ex.getNextException();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (con != null)
                    con.close();
            } catch (SQLException ex) {
                System.out.println("\nERROR:------ SQLException -----\n");
                System.out.println("Message: " + ex.getMessage());
                System.out.println("SQLState: " + ex.getSQLState());
                System.out.println("ErrorCode :" + ex.getErrorCode());
            }
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}